package com.yun.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.entity.ReturnBean;
import com.yun.entity.Tester;
import com.yun.service.TesterService;
import com.yun.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.filters.ExpiresFilter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Tester)表控制层
 *
 * @author makejava
 * @since 2021-11-25 09:45:30
 */
@RestController
@RequestMapping("tester")
@Slf4j
public class TesterController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TesterService testerService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param tester 查询实体
     * @return 所有数据
     */
    @RequestMapping("selectAllPage")
    public ReturnBean selectAllPage(Long page,Long limit, Tester tester) {
        if (page == null) {
            page = Constants.page;
            limit = Constants.limit;
        }
        Page<Tester> pageObj = new Page<>(page,limit);
        QueryWrapper<Tester> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(tester.getUsername())){
            queryWrapper.like("username",tester.getUsername());
        }
        if(ObjectUtil.isNotEmpty(tester.getPhone())){
            queryWrapper.like("username",tester.getPhone());
        }
        //Page<Tester> pageTester = this.testerService.page(pageObj, new QueryWrapper<>(tester));
        Page<Tester> pageTester = this.testerService.page(pageObj, queryWrapper);
        return super.success(pageTester.getRecords(),pageTester.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("findOne")
    public ReturnBean selectOne(Integer id) {
        return success(this.testerService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tester 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ReturnBean insert(@RequestBody Tester tester, HttpServletRequest request) {
        tester.setTestTime(new Date());
        HttpSession session = request.getSession();
        session.setAttribute("tester",tester);
        //插入到数据库中
        this.testerService.save(tester);
        //执行插入后tester里面有个id值，就不用根据手机号进行查询了
        //从数据库中查询到tester对象放到返回值中
        return success(tester);
    }

    /**
     * 修改数据
     *
     * @param tester 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Tester tester) {
        return success(this.testerService.updateById(tester));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("del")
    public ReturnBean delete(@RequestParam("idList[]") List<Long> idList) {
        //删除测试者
        return success(this.testerService.removeByIds(idList));
        //需要删除测试记录（用到事务）未完成
    }

    @PostMapping("resultSubmit")
    public ReturnBean resultSubmit(){

        return success(0,0L);
    }

    @RequestMapping("findByPhone")
    public ReturnBean findByPhone(String phone){
        Tester tester = testerService.checkPhone(phone);
        if(tester!=null){
            return success(null,0L);
        }else{
            return fail(null);
        }
    }

}

